package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.FoodInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodListBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.FoodInfoIface;
import com.example.lenovo.a203_yhy_ordering_3.iface.FoodLstIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodInfoListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodListListener;
import com.example.lenovo.a203_yhy_ordering_3.service.FoodInfoService;
import com.example.lenovo.a203_yhy_ordering_3.service.FoodListService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/12/1.
 */

public class FoodInfoModel implements FoodInfoIface{
    private Retrofit retrofit;


    public FoodInfoModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getFoodInfoResult(int food_id, final FoodInfoListener foodInfoListener) {
        FoodInfoService foodInfoService = retrofit.create(FoodInfoService.class);

        Call<FoodInfoBean> foodInfoCall = foodInfoService.getFoodById(food_id);

        foodInfoCall.enqueue(new Callback<FoodInfoBean>() {
            @Override
            public void onResponse(Call<FoodInfoBean> call, Response<FoodInfoBean> response) {
                if(response!=null)
                    foodInfoListener.onResponse(response.body());
                else
                    foodInfoListener.onFail("获取菜品详情失败");
            }

            @Override
            public void onFailure(Call<FoodInfoBean> call, Throwable t) {
                foodInfoListener.onFail(t.toString());
            }
        });
    }
}
